package com.tsd.jxc.sale.entity;

import com.tsd.core.utils.DoubleUtil;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.utils.ListUtil;
import com.tsd.jxc.goods.entity.JxcGoodsPriceExt;
import com.tsd.jxc.stock.entity.JxcStockInbillItemExt;
import com.tsd.jxc.vo.PrintVo;
import io.swagger.annotations.ApiModel;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

/**
 * 销售退货单-明细扩展
 *
 * @author Hillpool
 * @date 2022/07/29 15:04:27
 */
@ApiModel(description = "销售退货单-明细扩展")
public class JxcSaleReturnBillDetailExt extends JxcSaleReturnBillDetail implements Serializable {

    private List<String> return_list;

    private String goods_name;

    private Integer order_qty;

    private Integer return_qty;

    private String bill_state;

    private String sale_order_code;

    private BigDecimal unit_qty;

    private String spec_info;

    private List<JxcGoodsPriceExt> price_list;

    private BigDecimal remain_qty4base;

    public List<String> getReturn_list() {
        return return_list;
    }

    public void setReturn_list(List<String> return_list) {
        this.return_list = return_list;
    }

    public String getGoods_name() {
        return goods_name;
    }

    public void setGoods_name(String goods_name) {
        this.goods_name = goods_name;
    }

    public Integer getOrder_qty() {
        return order_qty;
    }

    public void setOrder_qty(Integer order_qty) {
        this.order_qty = order_qty;
    }

    public Integer getReturn_qty() {
        return return_qty;
    }

    public void setReturn_qty(Integer return_qty) {
        this.return_qty = return_qty;
    }

    public String getBill_state() {
        return bill_state;
    }

    public void setBill_state(String bill_state) {
        this.bill_state = bill_state;
    }

    public String getSale_order_code() {
        return sale_order_code;
    }

    public void setSale_order_code(String sale_order_code) {
        this.sale_order_code = sale_order_code;
    }

    public BigDecimal getUnit_qty() {
        return unit_qty;
    }

    public void setUnit_qty(BigDecimal unit_qty) {
        this.unit_qty = unit_qty;
    }

    public String getSpec_info() {
        return spec_info;
    }

    public void setSpec_info(String spec_info) {
        this.spec_info = spec_info;
    }

    public List<JxcGoodsPriceExt> getPrice_list() {
        return price_list;
    }

    public void setPrice_list(List<JxcGoodsPriceExt> price_list) {
        this.price_list = price_list;
    }

    public BigDecimal getRemain_qty4base() {
        return remain_qty4base;
    }

    public void setRemain_qty4base(BigDecimal remain_qty4base) {
        this.remain_qty4base = remain_qty4base;
    }

    public void calcUnitQty(BigDecimal qty4baseBig, BigDecimal qtyBig) {
        double qty4base = qty4baseBig == null ? 0D : qty4baseBig.doubleValue();
        double qty = qtyBig == null ? 0D : qtyBig.doubleValue();
        double unitQty = DoubleUtil.div(qty4base, qty, 4);
        this.setUnit_qty(BigDecimal.valueOf(unitQty));
    }

    public void calcGoodsPrice() {
        List<JxcGoodsPriceExt> priceExtList = this.getPrice_list();
        if (!HlpUtils.isEmptyList(priceExtList)) {
            JxcGoodsPriceExt unitPriceExt = ListUtil.get(priceExtList, t -> t.getUnit_name().equals(this.getUnit_name()));
            if (unitPriceExt != null) {
                this.setUnit_qty(BigDecimal.valueOf(unitPriceExt.getQty()));
                if (HlpUtils.isEmpty(this.getQty4base())) {
                    this.setQty4base(BigDecimal.valueOf(DoubleUtil.mul(this.getQty(), unitPriceExt.getQty())));
                }
            }
            double baseUnitPrice = DoubleUtil.div(this.getAmount(), this.getQty4base(), 4);
            for (JxcGoodsPriceExt priceExt : priceExtList) {
                priceExt.setSale_price(BigDecimal.valueOf(DoubleUtil.mul(baseUnitPrice, priceExt.getQty())));
            }
            this.setPrice_list(priceExtList);
        }
    }

    public JxcStockInbillItemExt genInBillItem() {
        JxcStockInbillItemExt itemExt = new JxcStockInbillItemExt();
        itemExt.setGoods_sid(this.getGoods_sid());
        itemExt.setSrc_sid(this.getSid());
        itemExt.setPrice(this.getPrice());
        itemExt.setIn_qty(this.getQty());
        itemExt.setAmount(this.getAmount());
        itemExt.setUnit_name(this.getUnit_name());
        itemExt.setGoods_name(this.getGoods_name());
        itemExt.setSn(this.getSn());
        return itemExt;
    }

    public PrintVo convert2Print() {
        PrintVo vo = new PrintVo();
        vo.setSn(this.getSn());
        vo.setGoods_name(this.getGoods_name());
        vo.setSpec_name(this.getSpec_info());
        vo.setQty(this.getQty());
        vo.setAmount(this.getAmount());
        vo.setNote_info(this.getNote_info());
        return vo;
    }
}
